package com.tfxy.hxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tfxy.hxl.R;
import com.tfxy.hxl.entity.RecommendationRecord;
import com.tfxy.hxl.mapper.RecommendationRecordMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推荐控制器（精简版）
 */
@RestController
@RequestMapping("/recommendation")
@Tag(name = "推荐管理", description = "推荐核心功能")
public class RecommendationController {

    @Autowired
    private RecommendationRecordMapper recommendationRecordMapper;

    /**
     * 1. 获取推荐列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户推荐")
    public R getUserRecommendations(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {

        QueryWrapper<RecommendationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .eq("deleted", 0)
                   .orderByDesc("create_time")
                   .last("LIMIT " + limit);

        List<RecommendationRecord> recommendations = recommendationRecordMapper.selectList(queryWrapper);
        return R.ok().data("recommendations", recommendations);
    }

    /**
     * 2. 记录推荐点击
     */
    @PostMapping("/click/{id}")
    @Operation(summary = "记录推荐点击")
    public R recordClick(@Parameter(description = "推荐记录ID") @PathVariable Long id) {
        UpdateWrapper<RecommendationRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                    .set("is_clicked", 1)
                    .set("click_time", new Date());

        int result = recommendationRecordMapper.update(null, updateWrapper);
        if (result > 0) {
            return R.ok().message("点击记录成功");
        } else {
            return R.error().message("点击记录失败");
        }
    }

    /**
     * 3. 获取推荐统计
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取推荐统计")
    public R getRecommendationStatistics() {
        QueryWrapper<RecommendationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        Long totalCount = recommendationRecordMapper.selectCount(queryWrapper);

        QueryWrapper<RecommendationRecord> clickWrapper = new QueryWrapper<>();
        clickWrapper.eq("deleted", 0).eq("is_clicked", 1);
        Long clickCount = recommendationRecordMapper.selectCount(clickWrapper);

        QueryWrapper<RecommendationRecord> enrollWrapper = new QueryWrapper<>();
        enrollWrapper.eq("deleted", 0).eq("is_enrolled", 1);
        Long enrollCount = recommendationRecordMapper.selectCount(enrollWrapper);

        Map<String, Object> statistics = new HashMap<>();
        statistics.put("totalCount", totalCount);
        statistics.put("clickCount", clickCount);
        statistics.put("enrollCount", enrollCount);
        statistics.put("clickRate", totalCount > 0 ? (double) clickCount / totalCount : 0.0);
        statistics.put("enrollRate", totalCount > 0 ? (double) enrollCount / totalCount : 0.0);

        return R.ok().data("statistics", statistics);
    }
}
